﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using ZG_member.BLL;
using System.Configuration;
using System.IO;

namespace ZG_member
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            this.Opacity = 0;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];

        }
      
        private void btnOK_Click(object sender, EventArgs e)
        {
            string username = txtUserName.Text.Trim();
            if (string.Equals(username, string.Empty))
            {
                MessageBox.Show("用户名错误");
                txtUserName.Focus();
                return;
            }
            string pwd = txtUserPwd.Text;
            {
                if (string.Equals(pwd, string.Empty))
                {
                    MessageBox.Show("密码错误");
                    txtUserPwd.Focus(); 
                    return;
                }
            }
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bt = Encoding.Default.GetBytes(pwd);
            bt = md5.ComputeHash(bt);
            pwd = BitConverter.ToString(bt).Replace("-", "");

         
            operatorInfo opeInfo = new operatorInfo();      
            StringBuilder sb = new StringBuilder();           
            sb.Append("username='" + txtUserName.Text.Trim()+"'");
            sb.Append("and"+" ");
            sb.Append("password='"+pwd+"'");
            DataTable dt = opeInfo.GetList(sb.ToString()).Tables[0];           
            if (dt.Rows.Count == 1)
            {
                //RechargeForm.userName = this.txtUserName.Text;
                //publishCardForm.userName = this.txtUserName.Text;

                string x;
                x = txtUserName.Text.Trim();
                StreamWriter sw = new StreamWriter("UserInfo.ini", false, Encoding.GetEncoding("gb2312"));
                sw.Flush();
                sw.Write(x);
                sw.Close();
                this.Close();
                DialogResult = DialogResult.OK;
               
                
                //Close();


                


            }
            else
            {
              MessageBox.Show("用户名和密码错误");
            }

            
           
          

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            
            this.timer1.Start();
            
        }

        private void Login_Activated(object sender, EventArgs e)
        {
          this.txtUserName.Focus();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }
        int flag = 0;
        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }  
     

      
        
    }
}
